<?php
    require APPPATH .'third_party/admin_controller'.EXT;
    class Ctv extends Admin_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
			$this->check_login_admin();
            $data = array();
            $this->title = "Home";  
			$id_item = $this->input->post('selected');
			$act = $this->input->post('act');
			if($act == "Delete Item")
			{
				if($id_item)
				{
					if(gettype($id_item) == 'array' && count($id_item)>0)
					{
					foreach($id_item as $row)
					 {
						  $this->ctv->delete_ctv($row);						
					 }
					   redirect('admin/ctv/');
					}
				}
			 
			}
            $data['result'] = $this->ctv->list_ctv();            
            $this->load_render($this->load->view('ctv/list',$data,TRUE));        
            
        }
        public function info_ctv()
        {
            $data = array();
            $uid = $this->uri->segment('4');
            $result = $this->ctv->get_id_ctv($uid);
            $data = array(
                'id'       =>$result->id_ctv,
                'fullname' =>$result->fullname,
                'username' =>$result->username,
                'email'    =>$result->email,
				'level'  =>$this->mains->get_name_level($result->level),
                'phone'    =>$result->phone,
                'address'  =>$result->address,
                'country'  =>$result->country,
                'avarta'   =>$result->avarta,
                'info'     =>$result->info,
                'gender'   =>$result->gender,
                'time_in'  =>$result->time_in,
                'time_out' =>$result->time_out,
                'active'   =>$result->active,
                'order_by' =>$result->order_by,
                'birthday' =>$result->birthday                 
            );           
            $this->load_render($this->load->view('ctv/info_ctv',$data,TRUE));
        }
        public function add_item()
		{
			$this->check_login_admin();
			$data = array();
            $email    = $this->input->post('txt_email',true);            
            $image 	  = $this->input->post('image',true);
			$act = $this->input->post('submit');
            $name = @$_FILES['txt_upload']['name'];  
            $id = $this->input->post('txt_id_ctv');
             // Kiem tra Email co ton tai hay chua
            $data = array(
                'fullname' => $this->input->post('txt_fullname',true),
                'username' => $this->input->post('txt_user',true),
                'password' => md5($this->input->post('txt_repass',true)),
                'email'    => $this->input->post('txt_email',true),
                'level'    => $this->input->post('txt_phanquyen',true),
                'phone'    => $this->input->post('txt_phone',true),
                'address'  => $this->input->post('txt_address',true),
                'country'  => $this->input->post('txt_country',true),
                'avarta'   => $name,
                'info'     => $this->input->post('txt_info',true),
                'gender'   => $this->input->post('txt_gender',true),
                'time_in'  => date('Y-m-d H:i:s'),
                'time_out' => date('Y-m-d H:i:s'),
                'birthday' => $this->input->post('txt_birthday',true)               
            );
            $uid = $this->uri->segment('4');  
            if($name !='')
            {
                    $config['upload_path'] = './upload/avarta/';
					$config['allowed_types'] = 'gif|jpg|png|jpeg';
					$config['remove_spaces'] = TRUE;
					$config['max_size'] = 1024;
					$config['max_width'] = '1024';
					$config['max_height'] = '1024';
					$config['file_name'] = $_FILES['txt_upload']['name'];
					$this->load->library('upload', $config);
                    $this->upload->initialize($config);
                    if(!$this->upload->do_upload('txt_upload')) 
					{
					return false;
					} else {
		            $upload = $this->upload->data();
					$data['image'] = 'upload/avarta/'.$upload['file_name'];
                    }                    
            }
            else
            {
                if($image !='') {
						$data['image'] = $image;					
						} else {
							$data['image'] = '';						
						}
            }
            if($act == "Submit")
            {
                if($id!='')
                 {
                    if($this->ctv->update_ctv($id,$data))
                    {
                        $data['error'] = "Cập nhật thành công";    
                        redirect('admin/ctv/');                 
                    }
                                   
                 } else {
                    if($this->ctv->create_ctv($data))
                    {
                        $data['error'] = "Cập nhật thành công";    
                        redirect('admin/ctv/');                 
                    }
                    
                 }
            }            
            $data['uid'] = $uid;
            $data['result'] = $this->ctv->get_id_ctv($uid);            
            $data['list_country'] = $this->mains->list_country();
            $this->load_render($this->load->view('members/add_item',$data,TRUE));
		
			
		}
        public function delete_ctv()
        {
            $uid = $this->uri->segment('4');
            $this->ctv->delete_ctv($uid);
            redirect('admin/ctv/');
        }
   }
?>       